//
//  MainTopCommonView.swift
//  GreenDeal
//
//  Created by Moennig Q on 14/12/2022.
//

import UIKit
import SDWebImage
import RxCocoa
import RxSwift
import SnapKit
class MainTopCommonView: UIView {
    lazy var disposeBag = DisposeBag()
    let backView = UIView()
    //MARK: - 用户头像
    let avatarImageView = UIImageView()
    //MARK: - 背景图
    let topBackImageView = UIImageView(image: R.image.installation_navigationbar_backimage())
    //MARK: - notification
    let notificationBtn = UIButton()
    //MARK: - notification unread badge
    let notificationRedBadgeView = UIView()
    //MARK: - 创建PVD
    let createJobBtn = UIButton()
    //MARK: - search
    let searchBtn = UIButton()
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    private func setUpView() {
        self.backgroundColor = UIColor(hexStr: Constant.SystemBackColor)
        //
        self.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //1.top view
        //back image view
        topBackImageView.contentMode = .scaleToFill
        backView.addSubview(topBackImageView)
        topBackImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        //me button
        avatarImageView.layer.cornerRadius = 20
        avatarImageView.clipsToBounds = true
        avatarImageView.contentMode = .scaleToFill
        backView.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { (make) in
            make.top.equalTo(UIDevice.getStatusBarHeight() + 20)
            make.left.equalTo(20)
            make.width.height.equalTo(40)
            make.bottom.equalTo(-155)
        }
        //MARK: - 通知
        notificationBtn.setImage(R.image.installation_list_notification(), for: UIControl.State.normal)
        backView.addSubview(notificationBtn)
        notificationBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-20)
            make.centerY.equalTo(avatarImageView)
            make.width.height.equalTo(40)
        }
        //通知的红点
        notificationRedBadgeView.backgroundColor = UIColor(hexStr: "#ED3E3E")
        notificationRedBadgeView.layer.cornerRadius = 4
        notificationRedBadgeView.clipsToBounds = true
        backView.addSubview(notificationRedBadgeView)
        notificationRedBadgeView.snp.makeConstraints { make in
            make.top.equalTo(notificationBtn).offset(8)
            make.left.equalTo(notificationBtn.snp.right).offset(-12)
            make.width.height.equalTo(8)
        }
        APPConfig.loginInfo.rx.observe(String.self, "avatarURLStr").subscribe { [weak self] urlStr in
            self?.setAvatarImage()
        }.disposed(by: self.disposeBag)
        //
        APPConfig.loginInfo.rx.observe(Int.self, "unReadNotificationCount").subscribe(onNext: {[weak self] count in
            let unReadCnt = count ?? 0
            self?.notificationRedBadgeView.isHidden = (unReadCnt > 0 ? false : true)
        }).disposed(by: self.disposeBag)
        //默认头像
        self.avatarImageView.image = R.image.me_default_avatar()
        createJobBtn.addTarget(self, action: #selector(createJobBtnClick), for: UIControl.Event.touchUpInside)
        notificationBtn.addTarget(self, action: #selector(notificationBtnClick), for: UIControl.Event.touchUpInside)
    }
    //MARK: - 是否显示create job， search btn
    func showOpertionBtns(showCreateJobBtn: Bool = true, showSearchBtn: Bool = true){
        if let _ = searchBtn.superview {
            searchBtn.removeFromSuperview()
        }
        if let _ = createJobBtn.superview {
            createJobBtn.removeFromSuperview()
        }
        avatarImageView.snp.remakeConstraints { (make) in
            make.top.equalTo(UIDevice.getStatusBarHeight() + 20)
            make.left.equalTo(20)
            make.width.height.equalTo(40)
            if showCreateJobBtn || showSearchBtn {//Installation tab和service job tab
                make.bottom.equalTo(-155)
            }else{//calendar tab
                make.bottom.equalToSuperview()
            }
        }
        //显示create job btn
        if showCreateJobBtn {
            createJobBtn.setImage(R.image.installation_list_add_icon(), for: UIControl.State.normal)
            backView.addSubview(createJobBtn)
            createJobBtn.snp.makeConstraints { (make) in
                make.right.equalTo(notificationBtn.snp.left).offset(-10)
                make.centerY.equalTo(avatarImageView)
                make.width.height.equalTo(notificationBtn)
            }
        }
        if showSearchBtn {
            if showCreateJobBtn {//放在create job btn 的左边
                //searchBtn
                searchBtn.setImage(R.image.installation_list_search(), for: UIControl.State.normal)
                backView.addSubview(searchBtn)
                searchBtn.snp.makeConstraints { (make) in
                    make.right.equalTo(createJobBtn.snp.left).offset(-10)
                    make.centerY.equalTo(avatarImageView)
                    make.width.height.equalTo(notificationBtn)
                }
            }else{//放在notification btn的左边
                //searchBtn
                searchBtn.setImage(R.image.installation_list_search(), for: UIControl.State.normal)
                backView.addSubview(searchBtn)
                searchBtn.snp.makeConstraints { (make) in
                    make.right.equalTo(notificationBtn.snp.left).offset(-10)
                    make.centerY.equalTo(avatarImageView)
                    make.width.height.equalTo(notificationBtn)
                }
            }
        }
    }
    private func setAvatarImage(){
        //设置头像
        SDWebImageManager.shared().imageCache?.clearMemory()//先清空内存缓存
        if let url = URL(string: APPConfig.loginInfo.avatarURLStr){
            self.avatarImageView.sd_setImage(with: url,placeholderImage: R.image.me_default_avatar(),options: SDWebImageOptions.cacheMemoryOnly)
        }else{
            self.avatarImageView.image = R.image.me_default_avatar()
        }
        
    }
    @objc func createJobBtnClick(){
        if let naviVC = self.firstNaviViewController(){
            let vc = CreateJobViewController()
            vc.hidesBottomBarWhenPushed = true
            naviVC.pushViewController(vc, animated: true)
        }
    }
    
    @objc private func notificationBtnClick(){
        if let naviVC = self.firstNaviViewController(){
            let vc = MessageListViewController()
            vc.hidesBottomBarWhenPushed = true
            naviVC.pushViewController(vc, animated: false)
        }
    }
}
